import { reqCategoryList, reqCategoryList1, reqCategoryList2, getAttrInfoList, deleteAttr, saveAttrInfo } from '@/api/attr/index'

const state = {
  caregorylist1: [],
  caregorylist2: [],
  caregorylist3: [],
  category3Id:'',
  attrInfor: [],
  attrFromInfo:[]
}
const mutations = {
  SET_CAREGORYLIST1(state, caregorylist1) {
    state.caregorylist1 = caregorylist1
  },
  SET_CAREGORYLIST2(state, caregorylist2) {
    state.caregorylist2 = caregorylist2
  },
  SET_CAREGORYLIST3(state, caregorylist3) {
    state.caregorylist3 = caregorylist3
  },
  SET_ATTRINFOR(state, attrInfor) {
    state.attrInfor = attrInfor
  },
  POST_ATTRINFOR(state,attrFromInfo){
    state.attrFromInfo = attrFromInfo
  },
  ADD_CAREGORY3(state,category3Id){
    state.category3Id = category3Id
  }
}

const actions = {
  //一级分类
  async getcaregory1List({ commit }) {
    try {
      let result = await reqCategoryList()
      console.log(result);
      if (result.code === 200) {
        commit('SET_CAREGORYLIST1', result.data)
        return 'ok'
      }
      return Promise.reject('获取一级分类失败')
    } catch (error) {
      return Promise.reject(error)
    }
  },
  //二级分类
  async getcaregory2List({ commit }, category1Id) {
    try {
      let result = await reqCategoryList1(category1Id)
      if (result.code === 200) {
        commit('SET_CAREGORYLIST2', result.data)
        return 'ok'
      }
      return Promise.reject('获取二级分类失败')
    } catch (error) {
      return Promise.reject(error)
    }
  },
  //三级分类
  async getcaregory3List({ commit }, category2Id) {
    try {
      let result = await reqCategoryList2(category2Id)
      if (result.code === 200) {
        commit('SET_CAREGORYLIST3', result.data)
        return 'ok'
      }
      return Promise.reject('获取三级分类失败')
    } catch (error) {
      return Promise.rejecte(error)
    }
  },
  //获取商品数据
  async getgetcaregoryListAll({ commit }, params) {
    try {
      let result = await getAttrInfoList(params)
      if (result.code === 200) {
        commit('ADD_CAREGORY3',params.category3Id)
        commit('SET_ATTRINFOR', result.data)
        return 'ok'
      }
      return Promise.reject('获取平台属性失败')
    } catch (error) {
      return Promise.rejecte(error)
    }
  },
  //删除单挑商品数据
  async deleteAttrInfo({ commit }, attrId) {
    try {
      let result = await deleteAttr(attrId)
      if (result.code === 200) {
        commit('SET_ATTRINFOR', result.data)
        alert('删除数据成功')
        return 'ok'
      }

      return Promise.reject('删除单挑数据失败')
    } catch (error) {
      return Promise.reject(error)
    }
  },
  //新增保存
  async onSavew({ commit },data) {
    try {
      let result = await saveAttrInfo(data)
      if(result.code === 200){
        commit('POST_ATTRINFOR',result.data)
        return 'ok'
      }
      return Promise.reject('添加属性失败')
    } catch (error) {
      return Promise.rejecte(error)
    }
  }
}
const getters = {

}


export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}